/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getReport,
  getYzList,
  getExportTask,
  asyncExport,
  regeneration,
  getAreaExportTask,
} from '@/services/report';
import { message, Modal } from 'antd';
import { isLegalData } from '@/utils/utils';

export default {
  namespace: 'report',

  state: {
    data: null,
    yzList: [],
  },

  effects: {
    *getExportTask({ payload, then }, { call }) {
      const response = yield call(getExportTask, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        Modal.info({
          title: '温馨提示',
          content: '导出数据申请已提交，请至【报表下载】处下载',
        });
      } else {
        message.error(msg);
      }
      if (then) {
        then(response);
      }
    },
    *getAreaExportTask({ payload, then }, { call }) {
      const response = yield call(getAreaExportTask, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code != 0) {
        message.error(msg);
      }
      if (then) {
        then(response);
      }
    },
    *asyncExport({ payload, then }, { call }) {
      const response = yield call(asyncExport, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        Modal.info({
          title: '温馨提示',
          content: '导出数据申请已提交，请至【报表下载】处下载',
        });
      } else {
        message.error(msg);
      }
      if (then) {
        then(response);
      }
    },
    *getReport({ payload }, { call, put }) {
      const response = yield call(getReport, payload);
      if (!response) return;
      const { code, data } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            data: isLegalData(data, []),
          },
        });
      }
    },
    *getYzList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getYzList, payload);
      if (!response) return;
      const { code, data } = response;
      const list = isLegalData(data, []);

      if (code == 0 && data) {
        yield put({
          type: 'save',
          payload: {
            yzList: list,
          },
        });
        __dva_resolve(list);
      } else {
        yield put({
          type: 'save',
          payload: {
            yzList: [],
          },
        });
        __dva_reject(response);
      }
    },
    *regeneration({ payload, then }, { call }) {
      const response = yield call(regeneration, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
      } else {
        message.error(msg);
      }
      if (then) {
        then(response);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
